<template>
	<uni-popup ref="syncLogPopup" type="top" :mask-click="false" style="top:100px;">
		<view class="modal popup-dialog">
			<uni-icons type="closeempty" class="close-btn" @click="closeDialog"></uni-icons>
			<view>
				<view class="uni-header" style="padding:0;">
					<view class="uni-group">
						<view class="uni-title">同步日志</view>
					</view>
				</view>
				<view style="padding:15px 0;">

					<uni-table ref="table" :loading="loading" emptyText="没有更多数据" border stripe>
						<uni-tr>
							<uni-th align="center">同步日期</uni-th>
							<uni-th align="center">AppID</uni-th>
							<uni-th align="center">同步状态</uni-th>
							<uni-th align="center">操作</uni-th>
						</uni-tr>

						<uni-tr v-for="(item,index) in tableData">
							<uni-td align="center">{{item.sync_date}}</uni-td>
							<uni-td align="center">{{item.appid}}</uni-td>
							<uni-td align="center">
								<text v-if="item.status == 0">未同步</text>
								<text v-if="item.status == 1">完成</text>
								<text v-if="item.status == 2">正在同步</text>
								<text v-if="item.status == 3">同步失败</text>
							</uni-td>
							<uni-td align="center" v-if="item.status == 1 || item.status == 2">
								-
							</uni-td>
							<uni-td align="center" v-else>
								<button type="primary" size="mini" @click="repush(item)">重新同步</button>
							</uni-td>
						</uni-tr>
					</uni-table>

				</view>
			</view>

		</view>
	</uni-popup>
</template>

<script>
	const db = uniCloud.database()
	const dbCmd = db.command
	import timeUtil from '@/uni_modules/uni-ad-admin/js_sdk/timeUtil'
	
	const adminObj = uniCloud.importObject('uni-ad-admin-receiver')
	export default {
		name: "uni-ad-sync-logs",
		data() {
			return {
				loading: false,
				tableName:'uni-ad-sync-logs',
				tableData:[]
			};
		},
		props: {
			ad_region: {
				type: Number,
				default: () => {
					return 0
				}
			},
			appid: {
				type: String,
				default: () => {
					return ''
				}
			}
		},
		methods: {
			open() {
				this.$refs.syncLogPopup.open()
				this.loadData()
			},
			closeDialog() {
				this.$refs.syncLogPopup.close()
			},
			repush(row){
				adminObj.repush(row).then(res=>{
					this.loadData()
				}).catch(err=>{
				})
			},
			loadData() {
				let endDate = timeUtil.plusDay(-30)
				let where = {
					ad_region: this.ad_region,
					appid: this.appid,
					sync_date: dbCmd.gt(endDate)
				}
				
				this.loading = true
				db.collection(this.tableName)
					.where(where)
					.orderBy('sync_date desc')
					.get()
					.then(res=>{
						this.loading = false
						let data = res.result.data
						let firstSyncDate = 0
						if(data && data.length){
							firstSyncDate = data[data.length-1].sync_date
						}
						
						let yestoday = timeUtil.yestoday()
						if(firstSyncDate === 0){
							firstSyncDate = yestoday
						}
						
						let start = timeUtil.toTime(firstSyncDate)
						let end = timeUtil.toTime(yestoday)
						let tableData = []
						for(let i=end;i>=start;i-=86400000){
							//console.log(i,timeUtil.timeFormat(i,'yyyyMMdd'))
							let cd = timeUtil.timeFormat(i,'yyyyMMdd')
								
							let find = data.find((item)=>{
								return item.sync_date == cd
							})
							if(!find){
								find = {
									appid: this.appid,
									sync_date: cd,
									status: 0,
									ad_region: this.ad_region
								}
							}
							tableData.push(find)
						}
						this.tableData = tableData
					})
			}
		}
	}
</script>

<style lang="scss">
	.popup-dialog {
		width: 650px;
		padding: 10px 20px 20px 20px;
		background-color: #FFF;
		position: relative;
	}

	.close-btn {
		cursor: pointer;
		position: absolute;
		top: 10px;
		right: 10px;
	}
	::v-deep .uni-table-scroll{
		height: 400px  !important;
		min-height: 400px !important;
		overflow-y: auto;
	}
</style>